<template>
  <a-drawer :title="title" :width="width" placement="right" :closable="false" @close="close" :visible="visible">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="大区名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['description', validatorRules.description]" placeholder="请输入大区名称"></a-input>
        </a-form-item>
        <a-form-item label="大区编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['dictionaryCode', validatorRules.dictionaryCode]" placeholder="请输入大区编码"></a-input>
        </a-form-item>
        <a-form-item label="排序值" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['sequenceId', validatorRules.sequenceId]" placeholder="请输入排序值"></a-input>
        </a-form-item>
      </a-form>
    </a-spin>
    <a-button type="primary" @click="handleOk" v-if="!disableSubmit">确定</a-button>
    <a-button type="primary" @click="handleCancel">取消</a-button>
  </a-drawer>
</template>

<script>
import { httpAction } from '@/api/manage'
import pick from 'lodash.pick'
import { validateDuplicateValue } from '@/utils/util'
import validate from '@/utils/validate'
import JDictSelectTag from '@/components/dict/JDictSelectTag'
import JSearchSelectTag from '@/components/dict/JSearchSelectTag'
export default {
  name: 'PerTemporaryTaskModal',
  components: {
    JDictSelectTag,
    JSearchSelectTag,
  },
  data() {
    return {
      form: this.$form.createForm(this),
      title: '操作',
      width: 800,
      visible: false,
      model: {},
      dateFormat: 'YYYY-MM-DD',
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        description: {
          rules: [{ required: true, message: '请输入大区名称!', trigger: 'blur' }],
        },
        dictionaryCode: {
          rules: [{ required: true, message: '请输入大区名称!', trigger: 'blur' }],
        },
        sequenceId: {
          rules: [{ required: true, message: '请输入大区名称!', trigger: 'blur' }],
        },
      },
      url: {
        add: '/online/api/createDataDictionary',
        edit: '/online/api/updateDataDictionary',
        get: '/online/api/getDataDictionary',
      },
      disableSubmit: false,
    }
  },
  created() {},
  methods: {
    add() {
      this.edit({})
    },
    edit(id) {
      this.visible = true
      let data = {}
      httpAction(this.url.get, { dictionaryId: id }).then((res) => {
        data = res.result
        this.form.resetFields()
        this.model = Object.assign({}, data)
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'description', 'dictionaryCode', 'sequenceId'))
        })
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleChange() {},
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.dictionaryId) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, values)
          formData.dictionaryTypeId = 'AREA_DEPARTMENT'
          console.log('表单提交数据', formData)
          httpAction(httpurl, formData, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    popupCallback(row) {
      this.form.setFieldsValue(pick(this.model, 'description'))
    },
  },
}
</script>

<style lang="less" scoped>
/** Button按钮间距 */
.ant-btn {
  margin-left: 30px;
  margin-bottom: 30px;
  float: right;
}
</style>
